package com.ucw.ucwclient.biz.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ucw.ucwclient.dal.dataobject.UItem;
import com.ucw.ucwclient.dal.dataobject.UItemCollect;
import com.ucw.ucwclient.dal.dataobject.UItemLike;
import com.ucw.ucwclient.dal.mapper.UItemCollectMapper;
import com.ucw.ucwclient.dal.mapper.UItemLikeMapper;
import com.ucw.ucwclient.dal.mapper.UItemMapper;
import com.ucw.ucwclient.dal.search.NormalSearch;

@Service
public class UItemService {

	@Resource
	private UItemMapper uItemMapper;

	@Resource
	private UItemCollectMapper uItemCollectMapper;

	@Resource
	private UItemLikeMapper uItemLikeMapper;

	public List<UItem> getList(NormalSearch search) {
		List<UItem> list = uItemMapper.selectByPageSearch(search);
		return list;
	}

	public UItem getOne(int id) {

		return uItemMapper.selectByPrimaryKey(id);
	}

	public Map<String, String> collectOne(Integer id, Long userId) {
		UItemCollect uc = new UItemCollect();
		uc.setCreateTime(new Date());
		uc.setItemId(id);
		uc.setUserId(userId);
		int result = uItemCollectMapper.insertSelective(uc);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> cancelCollectOne(Integer id, Long userId) {
		int result = uItemCollectMapper.deleteByPrimaryKey(id, userId);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> likeOne(Integer id, Long userId) {
		UItemLike like = new UItemLike();
		like.setItemId(id);
		like.setUserId(userId);
		like.setCreateTime(new Date());
		Map<String, String> resp = new HashMap<String, String>();
		int result = uItemLikeMapper.insertSelective(like);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> cancelLikeOne(Integer id, Long userId) {
		Map<String, String> resp = new HashMap<String, String>();

		int result = uItemLikeMapper.deleteByPrimaryKey(id, userId);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> reportOne(Integer id) {
		UItem item = new UItem();
		item.setId(id);
		item.setReport(true);
		Map<String, String> resp = new HashMap<String, String>();
		int result = uItemMapper.updateByPrimaryKeySelective(item);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> addOne(UItem ui, Long userId) {
		ui.setCreateTime(new Date());
		ui.setUserId(userId);
		Map<String, String> resp = new HashMap<String, String>();
		int result = uItemMapper.insertSelective(ui);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

}
